package com.salinity.kun.helper;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.util.Date;

import com.salinity.kun.model.ComparisonContent;
import com.salinity.kun.model.ComparisonItem;
import com.salinity.kun.model.FourierSeriesInput;
import com.salinity.kun.model.HydrologicalDataList;
import com.salinity.kun.model.HydrologicalDataList.DataItem;

public class ComparisonHelper {

	public static ComparisonContent[] getComparisonContentFromFile(
			Date startDate,
			Date endDate,
			double T0,
			double deltaT,
			int seriesCount
			) throws ParseException, IOException {
		// 取数据


		String[] StationNames = ConstantHelper.getStationNameArray();

		// 运算，并获取结果
		Double[] f;
		FourierSeriesInput fourierSeriesInput = new FourierSeriesInput(T0, deltaT, seriesCount);
		Integer itemCount = seriesCount;
		ComparisonContent[] comparisonContentArray = new ComparisonContent[itemCount];
		// 初始化数据，赋予级数 索引SeriesIndex 和周期T
		double tempT = 0.0;
		for (int i = 0; i < comparisonContentArray.length; i++) {
			comparisonContentArray[i] = new ComparisonContent();
			if (i != 0) {
				tempT = fourierSeriesInput.getT() / i;
			}
			comparisonContentArray[i].setSeriesIndex(i);
			comparisonContentArray[i].setT(tempT);
		}

		for (int sIndex = 0; sIndex < StationNames.length; sIndex++) {
			HydrologicalDataList hydrologicalDataList = CalculationHelper.getDataByDateFromFile(startDate,
					endDate, StationNames[sIndex]);

			// 每个站点添加一条记录
			for (int i = 0; i < comparisonContentArray.length; i++) {
				comparisonContentArray[i].getComparisonItemList().add(new ComparisonItem(StationNames[sIndex]));
			}

			for (DataItem dataItem : DataItem.values()) {
				if (dataItem.equals(DataItem.DATE))
					continue;
				// get values of this item as Double Array
				f = (Double[]) hydrologicalDataList.getItem(dataItem);
				// set to fourierSeriesInput
				fourierSeriesInput.setF(f);
				setValueToComparisonContentArray(fourierSeriesInput, sIndex, StationNames[sIndex], dataItem,
						comparisonContentArray);
			}
		}
		return comparisonContentArray;
	}

	public static void writeToFile(ComparisonContent[] comparisonContentArray,Path outputPath) throws IOException{
		if(outputPath.toFile().exists()){
			outputPath.toFile().delete();
		}
		if(!outputPath.getParent().toFile().exists()){
			outputPath.getParent().toFile().mkdirs();
		}
		outputPath.toFile().createNewFile();
		
		try(BufferedWriter bw = Files.newBufferedWriter(outputPath, StandardOpenOption.WRITE)){
			StringBuilder sb = new StringBuilder();
			
			for(int i = 0;i< comparisonContentArray.length;i++){			
				sb.delete(0, sb.length());
				sb.append("SeriesIndex,").append(comparisonContentArray[i].getSeriesIndex())
				.append("\n");
				sb.append("T,").append(comparisonContentArray[i].getT()).append("\n");
				sb.append("Station,tide,salinity,velocity").append("\n");
				
				for(ComparisonItem comparisonItem:comparisonContentArray[i].getComparisonItemList()){
					sb.append(comparisonItem.getStationId()).append(",")
					  .append(comparisonItem.getTide()).append(",")
					  .append(comparisonItem.getSaliniy()).append(",")
					  .append(comparisonItem.getVelocity()).append("\n");	
				}
				
				bw.write(sb.toString());
				bw.newLine();
				bw.newLine();
				bw.newLine();
			}			
		}
		
		
	}
	

	private static void setValueToComparisonContentArray(FourierSeriesInput fourierSeriesInput, int sIndex,
			String stationName, DataItem dataItem, ComparisonContent[] comparisonContentArray) {
		if (comparisonContentArray == null) {
			throw new RuntimeException("comparison content array can not be null.");
		}
		FourierSeriesHelper fsHelper = new FourierSeriesHelper(fourierSeriesInput);

		switch (dataItem) {
		case H:
			for (int i = 0; i < comparisonContentArray.length; i++) {
				comparisonContentArray[i].getComparisonItemList().get(sIndex).setTide(fsHelper.get_fs(i));
			}
			break;
		case SALINITY_AVG:
			for (int i = 0; i < comparisonContentArray.length; i++) {
				comparisonContentArray[i].getComparisonItemList().get(sIndex).setSaliniy(fsHelper.get_fs(i));
			}
			break;
		case VELOCITY_AVG:
			for (int i = 0; i < comparisonContentArray.length; i++) {
				comparisonContentArray[i].getComparisonItemList().get(sIndex).setVelocity(fsHelper.get_fs(i));
			}
			break;
		case DATE:
			break;
		default:
			break;
		}
	}

	


}
